﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using Common;
using Model;
using PLSServer;
using PositionServer.Service;

namespace AcceptServer.Service
{
    class PhoneUserProtocol
    {
       
        OperateDataBase operateDataBase = new OperateDataBase();

        private PositionService positionService = new PositionService();

        public byte[] PhoneUserRegister(byte[] realData, IPAddress clientIp)
        {

            byte[] phoneNumberbyts = new byte[6];
            Array.Copy(realData, 5, phoneNumberbyts, 0, 6);
            byte[] serialNumber = new byte[2];
            Array.Copy(realData, 11, serialNumber, 0, 2);


            //0-7  IMEI 8byte
            byte[] IMEI_bytes = new byte[8];
            Array.Copy(realData, ConstParameters.MBodySatrtIndex, IMEI_bytes, 0, 8);
            string IMEI = CommonRoutine.ByteToBcdString(IMEI_bytes);
            IMEI = IMEI.Remove(0, 1);

            //8-11  4byte  Version
            byte[] version_bytes = new byte[4];
            Array.Copy(realData, ConstParameters.MBodySatrtIndex + 8, version_bytes, 0, 4);
            Array.Reverse(version_bytes);
            int nowVersion = BitConverter.ToInt32(version_bytes, 0);

            //------------------------将手机用户登陆录入数据库
            string phoneNum = OperateAgreement.GetPhoneNumInMessageHead(realData);
            PhoneUser model = operateDataBase.PhoneUserGetModelByPhoneNumber(phoneNum);
            if (model != null)
            {
                model.LastLoginTime = DateTime.Now;
                model.Imei = IMEI;
                bool result = operateDataBase.PhoneUserUpdate(model);
            }
            else  //数据库无记录  
            {
                model = new PhoneUser();
                model.PhoneNumber = phoneNum;
                model.Imei = IMEI;
                model.RegistTime = DateTime.Now;
                model.LastLoginTime = DateTime.Now;
                bool result = operateDataBase.PhoneUserAdd(model);
            }
            
            List<byte> body = new List<byte>();
            try
            { 
                body.Add(1);                              
                string version = AcceptMainForm.PhoneUserClientVersion;
                byte[] vBytes = Encoding.GetEncoding("GBK").GetBytes(version);
                body.AddRange(vBytes);  
            }
            catch (Exception ex)
            {
                CommonRoutine.HandlingExceptions(ex);
                LogService.WriteLog("PhoneUserProtocol.cs-Regist");
                body.Clear();
                body.Add(0);
            }

            byte[] messageHead = OperateAgreement.GetMessageHead(
                ConstParameters.CmdPhoneUserRegisterReply,
                false, false, body.Count, phoneNumberbyts,
                                                serialNumber, 0, 0);
            byte[] tempData = OperateAgreement.PackageSendData(messageHead, body.ToArray());
             
            return tempData; 
        }
         

    }
}
